from dvadmin.utils.serializers import CustomModelSerializer
from rest_framework import serializers

from .models import AssetType, AssetStatus, SeAssets


class AssetStatusSerializer(CustomModelSerializer):
    """资产状态序列化器"""
    class Meta:
        model = AssetStatus
        fields = "__all__"


class AssetTypeSerializer(CustomModelSerializer):
    """资产类型序列化器"""
    class Meta:
        model = AssetType
        fields = "__all__"

class SeAssetsSerializer(CustomModelSerializer):
    """资产序列化器"""
    
    class Meta:
        model = SeAssets
        fields = "__all__"

class SeAssetsCreateUpdateSerializer(CustomModelSerializer):
    """资产创建更新序列化器"""

    class Meta:
        model = SeAssets
        fields = "__all__"

class SeAssetsImportSerializer(CustomModelSerializer):
    """资产导入序列化器"""

    asstatus = serializers.CharField(source='asstatus.title', default= None, help_text="资产状态")
    astype = serializers.CharField(source='astype.title', default= None, help_text="资产类型")

    class Meta:
        model = SeAssets
        fields = "__all__"

    def run_validation(self, data={}):
        if type(data) is dict:
            data['astype'] = AssetType.objects.get(title=data.get('astype')).id
            data['asstatus'] = AssetStatus.objects.get(title=data.get('asstatus')).id
        return super().run_validation(data)

class SeAssetsExportSerializer(CustomModelSerializer):
    """资产导出序列化器"""

    asstatus = serializers.CharField(source='asstatus.title', default= None, read_only=True, help_text="资产状态")
    astype = serializers.CharField(source='astype.title', default= None, read_only=True, help_text="资产类型")
    
    class Meta:
        model = SeAssets
        fields = "__all__"
